/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.SdBank;

import com.SySammy.SdBank.Prop.BankProperties;
import java.util.List;

/**
 *
 * @author Sammy
 */
public class BankManager {

    sdB plugin;

    public BankManager(sdB aThis) {
        this.plugin = aThis;
    }

    private Bank OpenDataBase(String p) {
        Bank persistenceClass = plugin.getDatabase().find(Bank.class).where().ieq("PlayerName", p).findUnique();
        if (persistenceClass == null) {
            persistenceClass = new Bank();
        }
        return persistenceClass;
    }

    public boolean HasLoan(String p) {
        Bank persistenceClass = plugin.getDatabase().find(Bank.class).where().ieq("PlayerName", p).findUnique();
        if (persistenceClass == null) {
            return false;
        }
        return true;
    }

    public void LoanMaker(String p, int amount, int time, int ID, int IDrep) {
        Bank plyReg = OpenDataBase(p);
        plyReg.setPlayerName(p);
        plyReg.setAmount(amount);
        plyReg.setRepaid((int) (amount + ((int) amount * (BankProperties.usury / 100))));
        plyReg.setLoanTime(time*1440);
        plyReg.setTimeLeft(time*1440);
        plyReg.setTime1970(System.currentTimeMillis());
        plyReg.setScheduleID(ID);
        plyReg.setScheduleRepID(IDrep);
        plugin.getDatabase().save(plyReg);
    }

    public List<Bank> getLoans() {
        return plugin.getDatabase().find(Bank.class).where().findList();
    }

    public int getAmount(String p) {
        Bank plyReg = OpenDataBase(p);
        return plyReg.getAmount();
    }

    public int getRepaid(String p) {
        Bank plyReg = OpenDataBase(p);
        return plyReg.getRepaid();
    }

    public int getTime(String p) {
        Bank plyReg = OpenDataBase(p);
        return plyReg.getLoanTime();
    }

    public long getTimeLeft(String p) {
        Bank plyReg = OpenDataBase(p);
        return plyReg.getTimeLeft();
    }

    public int getScheduleID(String p) {
        Bank plyReg = OpenDataBase(p);
        return plyReg.getScheduleID();
    }

    public int getScheduleRepId(String p) {
        Bank plyReg = OpenDataBase(p);
        return plyReg.getScheduleRepID();
    }

    public void setTimeLeft(String p) {
        Bank plyReg = OpenDataBase(p);
        long oldTime = plyReg.getTimeLeft();
        long TimeLeft = (plyReg.getLoanTime() - ((System.currentTimeMillis() - plyReg.getTime1970()) / (60*1000)));
        plyReg.setTimeLeft(TimeLeft);
        plugin.getDatabase().save(plyReg);
    }

    public void payLoan(String p, int amount) {
        Bank plyReg = OpenDataBase(p);
        int oldPay = plyReg.getRepaid();
        int newPay = oldPay - amount;
        plyReg.setRepaid(newPay);
        plugin.getDatabase().save(plyReg);
    }

    public void setSchedulers(String p, int Sch, int RepSch) {
        Bank plyReg = OpenDataBase(p);
        plyReg.setScheduleID(Sch);
        plyReg.setScheduleRepID(RepSch);
        plugin.getDatabase().save(plyReg);
    }

    public void Remove(String p) {
        Bank plyReg = OpenDataBase(p);
        plugin.getDatabase().delete(Bank.class, plyReg.getPlayerId());
    }
}
